//
//  TOTools.m
//  B段项目
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "TOTools.h"

@implementation TOTools

+(CGSize)labelRectWithSize:(CGSize)size
                 LabelText:(NSString *)labelText
                      Font:(UIFont *)font{
    
    NSDictionary  *dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    CGSize actualsize = [labelText boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    return actualsize;
    
}

+(UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize
{
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    
    UIGraphicsBeginImageContext(targetSize);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointZero;
    thumbnailRect.size.width  = targetSize.width;
    thumbnailRect.size.height = targetSize.height;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage ;
}

+(MJRefreshGifHeader *)createTableViewGifHeaderWithTarget:(id)target Action:(SEL)action
{
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:target refreshingAction:action];
    header.stateLabel.hidden = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    NSMutableArray *loadImageArr = [NSMutableArray array];
    for (int i = 0; i < 28; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"refresh%d", i]];
        
        image = [TOTools image:image byScalingToSize:CGSizeMake(image.size.width/3, image.size.height/3)];
        
        [loadImageArr addObject:image];
    }
    [header setImages:loadImageArr forState:MJRefreshStateRefreshing];
    UIImage *image1 = [UIImage imageNamed:@"pullRefresh"];
    UIImage *image2 = [UIImage imageNamed:@"loading"];
    
    image1 = [TOTools image:image1 byScalingToSize:CGSizeMake(image1.size.width/3, image1.size.height/3)];
    image2 = [TOTools image:image2 byScalingToSize:CGSizeMake(image2.size.width/3, image2.size.height/3)];
    [header setImages:[NSArray arrayWithObjects:image1, nil] forState:MJRefreshStateIdle];
    [header setImages:[NSArray arrayWithObjects:image1, nil] forState:MJRefreshStatePulling];
    [header setImages:[NSArray arrayWithObjects:image2, nil] forState:MJRefreshStateWillRefresh];
    
    return header;
}

@end
